/*  GMooc - A Java web application for delivering massive open online courses
*   Copyright (C) 2012 Charles A. Wight
*   
*    This program is free software: you can redistribute it and/or modify
*   it under the terms of the GNU General Public License as published by
*   the Free Software Foundation, either version 3 of the License, or
*   (at your option) any later version.
*
*   This program is distributed in the hope that it will be useful,
*   but WITHOUT ANY WARRANTY; without even the implied warranty of
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*   GNU General Public License for more details.
*
*   You should have received a copy of the GNU General Public License
*   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package org.gmooc;

import java.io.IOException;
import java.io.PrintWriter;

import javax.persistence.Transient;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.googlecode.objectify.Objectify;

@SuppressWarnings("serial")
public class Home extends HttpServlet {
	
	@Transient static UserService userService = UserServiceFactory.getUserService();
	Objectify ofy = new DAO().ofy();
	
	static String header(User user) {
		StringBuffer buf = new StringBuffer();
		buf.append("<html><head><title>Gmooc</title></head><body>");
		buf.append("<table style='width:100%'><tr style='vertical-align:top'><td><a href='/'><img src=/images/gmooc-logo-small.png alt='gmooc.org'></a></td>"
				+ "<td><a href='/'>Home</a>&nbsp;&nbsp;<a href='/about'>About</a>&nbsp;&nbsp;" + (user==null?"":"<a href=/profile>Profile</a>&nbsp;&nbsp;") + "</td>"
				+ (user!=null && user.isAdministrator()?"<td><a href='/admin'>Admin</a></td>":"")
				+ "<td style='text-align:right'>" 
				+ (user==null?"<a href=" + userService.createLoginURL("/login") + "><b>Sign In</b></a>":"<b>" + user.getEmail() + "</b>&nbsp;&nbsp;&nbsp;<a href='/logout'>Sign Out</a>") 
				+ "</b></td></tr></table>");

		return buf.toString();
	}
	
	public static String footer = "\n<br><hr><span style='font-size:smaller'><table style='width:100%;border-spacing: 20px 0px'><tr>"
		+ "<td>&copy; 2012 Chuck Wight <a rel='license' href='http://creativecommons.org/licenses/by/3.0/'>"
		+ "<img alt='Creative Commons License' style='border-width:0' src='http://i.creativecommons.org/l/by/3.0/80x15.png' /></a></td>"
		+ "<td align=center><a href='/about#terms'>Terms and Conditions of Use</a></td>"
		+ "<td align=right><a href='http://code.google.com/appengine/'><img src=/images/GAE.gif border=0 "
		+ "alt='Powered by Google App Engine'></a></td></tr></table>"
		+ "</span>"
		+ "</TD></TR></TABLE>\n"
		+ "</body></html>";
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		User user = (User)request.getSession(true).getAttribute("User");
		
		out.println(homePage(user));
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws IOException {
		doGet(request,response);
	}

	String homePage(User user) {
		StringBuffer buf = new StringBuffer(header(user));
		try {
			buf.append("<h2>Welcome to Gmooc</h2>");
			buf.append("<span style='color:red'>This site is currently under construction.</span><p>");
			buf.append("Gmooc is an open source project specifically designed for offering massive open online courses (MOOCs). "
					+ "Recent expeience has shown that popular MOOCs can have 100,000 students or more, so special "
					+ "techniques are required to ensure that<ul>"
					+ "<li>the software can scale to essentially infinite size"
					+ "<li>large amounts of data can be read from and written to the database without collisions"
					+ "<li>students can self-organize into study groups to assist each other"
					+ "<li>only the most severe issues get elevated for attention by the course instructor"
					+ "</ul>");
		} catch (Exception e) {
			buf.append(e.toString());
		}
		return buf.toString() + footer;
	}
	
}
